package independentsetproblem;

import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import java.util.ArrayList;

public class Main
{
    public static void main(String[] args) 
    {
         String importFile;
         if(args.length == 0)
         {
            importFile = "test.dot";
         }
         else
         {
            importFile = args[0];
         }
         DotImport importFromDot = new DotImport(importFile);
         importFromDot.importGraph();
         if(importFromDot.isCorrect())
         {
            //clique in a complementary graph is an independent set
            UndirectedGraph<Vertex, DefaultEdge> sourceGraph = importFromDot.getGraph();
            UndirectedGraph<Vertex, DefaultEdge> complGraph = complementary(sourceGraph);
            
            long startTime = System.currentTimeMillis();

            Cliques allCliques = new Cliques(complGraph);
            System.out.println("\n"+"Search started...");
            ArrayList<Vertex> v = new ArrayList<Vertex>(complGraph.vertexSet());
            allCliques.expand(v, v);

            long finishTime = System.currentTimeMillis();
            double workTime = (double)(finishTime - startTime)/1000;

            allCliques.markClique();
            DotExport exportToDot = new DotExport(allCliques.getGraph(),"result.dot");
            exportToDot.export();
            allCliques.result();
            System.out.println("Algorithm execution time: "+  workTime + " sec.");
         }
    }

    private static UndirectedGraph<Vertex, DefaultEdge> complementary(UndirectedGraph<Vertex, DefaultEdge> g)
    {
        System.out.println("\n"+ "Complementary graph is being found");
        UndirectedGraph<Vertex, DefaultEdge> target =
                new SimpleGraph<Vertex, DefaultEdge>(DefaultEdge.class);
        ArrayList<Vertex> vertices = new ArrayList<Vertex>(g.vertexSet());
        
        for(Vertex v : vertices)
        {
            target.addVertex(v);
        }
        for(int i =0; i< vertices.size();i++)
        {
            for(int j = i+1; j< vertices.size();j++)
            {
                if(!g.containsEdge(vertices.get(i), vertices.get(j)))
                {
                    target.addEdge(vertices.get(i), vertices.get(j));
                }
            }
        }
        return target;
    }
}
